package com.example.base.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageButton;
import android.widget.ListView;

import com.example.base.R;
import com.example.base.adapter.MyAdapter2;
import com.example.base.bean.Home;
import com.example.base.bean.HomeThree;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class CollectActivity extends AppCompatActivity {
  private ListView listView;
  private final List<HomeThree> dataList = new ArrayList<>();
  private ImageButton imageButton;
  private final String TAG = "CollectActivity";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_collect);
    initView();
    initListen();
    getDataFromNet();
  }

  private void getDataFromNet() {
    SharedPreferences sp = getSharedPreferences("cookies", Context.MODE_PRIVATE);
    String value1 = sp.getString("loginUserName_wanandroid_com", "null");
    String value2 = sp.getString("loginUserName", "null");
    String value3 = sp.getString("token_pass_wanandroid_com", "null");
    String value4 = sp.getString("token_pass", "null");
    OkHttpClient.Builder client = new OkHttpClient.Builder();
    Request request = new Request.Builder()
        .url("https://www.wanandroid.com/lg/collect/list/0/json")
        .get()
        .addHeader("cookie", value1)
        .addHeader("cookie", value2)
        .addHeader("cookie", value3)
        .addHeader("cookie", value4)
        .build();
    Call call = client.build().newCall(request);
    call.enqueue(new Callback() {
      @Override
      public void onFailure(@NotNull Call call, @NotNull IOException e) {
      }

      @Override
      public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
        if (response.isSuccessful()) {
          Gson gson = new Gson();
          if (response.body() == null) {
            Log.d(TAG, "response.body() isNull");
            return;
          }
          Home home = gson.fromJson(response.body().string(), Home.class);
          runOnUiThread(() -> {
            refreshListView(home);
            listView.setOnItemClickListener((parent, view, position, id) -> {
              Intent intent = new Intent(CollectActivity.this, WebViewActivity.class);
              intent.putExtra("Url", home.getData().getDatas().get(position).getLink());
              startActivity(intent);
            });
          });
        }

      }

    });

  }

  private void refreshListView(Home home) {
    List<HomeThree> data = home.getData().getDatas();
    MyAdapter2 mAdapter = new MyAdapter2(this, dataList);
    listView.setAdapter(mAdapter);
    mAdapter.setData(data);
    mAdapter.notifyDataSetChanged();
  }

  private void initListen() {
    imageButton.setOnClickListener(v -> finish());

  }

  private void initView() {
    imageButton = findViewById(R.id.img_collect_back);
    listView = findViewById(R.id.list_collect);
  }
}